package com.smilive.start.netty.chat.peotocol;

import com.smilive.start.netty.chat.message.PingMessage;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @program: netty
 * @Date: 2022-01-10 15:23
 * @Author: smilive
 * @Description:  因为心跳信息使用了自定义协议,所以该Handle 记得放在自定义协议处理器后面,出站的时候才可以处理
 */
public class ClientChannelDuplexHandler extends ChannelDuplexHandler {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.WRITER_IDLE) {
            // 发送心跳包
            ctx.writeAndFlush(new PingMessage());
        }
    }
}
